#include "downloadHeaders/downloadmodelh/historyitemmodel.h"
#include "downloadHeaders/downloadmodelh/connection.h"
#include <QSqlQuery>
#include <QMetaEnum>
#include <QIcon>
#include <QFileInfo>
#include <QTemporaryFile>
#include <QDir>
#include <QFileIconProvider>
#include <QCoreApplication>
historyitemmodel::historyitemmodel(QObject *parent):
    QObject(parent)
{
    createConnection();
}

void historyitemmodel::deleteRecord()
{
    QSqlQuery query;
    query.prepare("delete from download where downloadFile = ?;");
    query.addBindValue(downloadFileDir + downloadFileName);
    if(query.exec())
        qDebug()<<"成功删除"<<downloadFileDir + downloadFileName;
    else
        qDebug()<<"删除失败";

}
void historyitemmodel::deleteAll()
{
    QSqlQuery query;
    if(query.exec("delete from download where downloadFile != 'DICTIONARY' ;"))
        qDebug()<<"全部删除";
}
void historyitemmodel::insertRecord()
{
    QSqlQuery query;
    QString current_date =curTime.toString("yyyy-MM-dd hh:mm:ss");
    QMetaEnum metaEnum = QMetaEnum::fromType<historyitemmodel::DownloadState>();
    QString state =  metaEnum.valueToKey(currentState);
    query.prepare("insert into download values(?,?,?,?,?,?,?,?);");
    query.addBindValue(downloadFileDir + downloadFileName);
    query.addBindValue(downloadFileName);
    query.addBindValue(downloadFileDir);
    query.addBindValue(url);
    query.addBindValue(current_date);
    query.addBindValue(totalBytes);
    query.addBindValue(state);
    query.addBindValue(account);

    if(query.exec())
        qDebug()<<"插入成功！！！！！";
   else
        qDebug()<<"插入失败";

}
void historyitemmodel::updateStateToDelete()
{
    currentState = DownlaodDeleted;
    QMetaEnum metaEnum = QMetaEnum::fromType<historyitemmodel::DownloadState>();
    QString state =  metaEnum.valueToKey(currentState);
    QSqlQuery query;
    query.prepare("update download set currentState = ? where downloadFile = ?;");
    query.addBindValue(state);
    query.addBindValue(downloadFileDir + downloadFileName);

    query.exec();
}
QSqlQuery* historyitemmodel::conductQuery()
{
    QSqlQuery *query = new QSqlQuery();
    if(query->exec("select * from download where downloadFile != 'DICTIONARY';"))
        qDebug()<<"查询全表成功";
    else
        qDebug()<<"查询全表失败";
    return query;
}

bool historyitemmodel::downloadDirValid()
{
    QSqlQuery query;
    query.prepare("select downloadFileDir from download where downloadFile = ?;");
    query.addBindValue("DICTIONARY");
    if(query.exec())
        qDebug()<<"成功查询下载目录";
    while(query.next())
    {
        if(query.value(0).toString() == QString::fromLocal8Bit("NoDownloadDir"))
            return false;
    }
    return true;
}
QString historyitemmodel::getDownloadDir()
{
    QSqlQuery query;
    query.prepare("select downloadFileDir from download where downloadFile = ?;");
    query.addBindValue("DICTIONARY");
    if(query.exec())
        qDebug()<<"得到下载目录";
    query.first();
        qDebug()<<query.value(0).toString();
        return query.value(0).toString();
}
void historyitemmodel::changeDownloadDir(QString dir)
{
    QSqlQuery query;
    query.prepare("update download set downloadFileDir = ? where downloadFile = ?;");
    query.addBindValue(dir);
    query.addBindValue("DICTIONARY");
    if(query.exec())
        qDebug()<<"改变下载目录";
    qDebug()<<getDownloadDir();
}


QIcon historyitemmodel::getIconByFileEx()
{
    QFileInfo fileinfo = QFileInfo(downloadFileName);

    QString extension = fileinfo.suffix();
    QFileIconProvider provider;
    QIcon icon;
    QString strTemplateName = QDir::tempPath() + QDir::separator() +
            QCoreApplication::applicationName() + "_XXXXXX." + extension;

    QTemporaryFile tmpFile(strTemplateName);
    tmpFile.setAutoRemove(false);

    if (tmpFile.open())
    {
        tmpFile.close();
        icon = provider.icon(QFileInfo(strTemplateName));
        // tmpFile.remove();
    }
    else
    {
        qCritical() << QString("failed to write temporary file %1").arg(tmpFile.fileName());
    }

    return icon;
}

